package com.ruoyi.project.system.grouping.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.grouping.domain.Grouping;
import com.ruoyi.project.system.grouping.service.IGroupingService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-04-13
 */
@Controller
@RequestMapping("/system/grouping")
public class GroupingController extends BaseController
{
    private String prefix = "system/grouping";

    @Autowired
    private IGroupingService groupingService;

    @RequiresPermissions("system:grouping:view")
    @GetMapping()
    public String grouping()
    {
        return prefix + "/grouping";
    }

    /**
     * 查询【请填写功能名称】列表
     */
//    @RequiresPermissions("system:grouping:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Grouping grouping)
    {
        startPage();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if ("admin".equals(loginUser)){
            List<Grouping> list = groupingService.selectGroupingList(grouping);
            return getDataTable(list);
        }else {
            List<Grouping> groupingByUserName = groupingService.selectByUserName(loginUser);
            return getDataTable(groupingByUserName);
        }
//        List<Grouping> list = groupingService.selectGroupingList(grouping);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @RequiresPermissions("system:grouping:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Grouping grouping)
    {
        List<Grouping> list = groupingService.selectGroupingList(grouping);
        ExcelUtil<Grouping> util = new ExcelUtil<Grouping>(Grouping.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
//    @RequiresPermissions("system:grouping:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Grouping grouping)
    {
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        String name = grouping.getName();
        Grouping groupingAdd = new Grouping();
        groupingAdd.setName(name);
        groupingAdd.setUserName(loginUser);
        return toAjax(groupingService.insertGrouping(groupingAdd));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @RequiresPermissions("system:grouping:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Grouping grouping = groupingService.selectGroupingById(id);
        mmap.put("grouping", grouping);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
//    @RequiresPermissions("system:grouping:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Grouping grouping)
    {
        return toAjax(groupingService.updateGrouping(grouping));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @RequiresPermissions("system:grouping:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(groupingService.deleteGroupingByIds(ids));
    }
}
